package com.ssm.reader.controller;

import com.ssm.reader.entity.Evaluation;
import com.ssm.reader.entity.Member;
import com.ssm.reader.service.MemberService;
import com.ssm.reader.service.exception.BussinessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/*
 *
 *
 *@data 2022-05-01
 */
@Controller
public class MemberController {
    @Resource
    private MemberService memberService;

    @GetMapping("/register.html")
    public ModelAndView showRegister(){
        return new ModelAndView("/register");

    }

    @PostMapping("/registe")
    @ResponseBody
    //返回状态信息代码和消息
    public Map registe(String vc, String username, String password, String nickname, HttpServletRequest httpServletRequest){
        String  kaptchaVerifyCode = (String)httpServletRequest.getSession().getAttribute("kaptchaVerifyCode");
        Map result = new HashMap();
        if (vc == null || kaptchaVerifyCode == null || !vc.equalsIgnoreCase(kaptchaVerifyCode)){
            result.put("code","VC01");
            result.put("msg","验证码错误");

        }else {
            //验证码对了，就开始创建用户
            try {
                memberService.createMember(username,password,nickname);
                result.put("code","0");
                result.put("msg","success");
            } catch (BussinessException e) {
                e.printStackTrace();
                result.put("code",e.getCode());
                result.put("msg",e.getMsg());
            }
        }

        return result;

    }

    @GetMapping("/login.html")
    public ModelAndView showLogin(){
        ModelAndView modelAndView = new ModelAndView("/login");
        return modelAndView;
    }

    @PostMapping("/check_login")
    @ResponseBody
    public Map checkLogin(String username, String password, String vc, HttpSession session){

        String  kaptchaVerifyCode = (String)session.getAttribute("kaptchaVerifyCode");
        Map result = new HashMap();
        if (vc == null || kaptchaVerifyCode == null || !vc.equalsIgnoreCase(kaptchaVerifyCode)){
            result.put("code","VC01");
            result.put("msg","验证码错误");

        }else {
            try{
                Member member = memberService.checkLogin(username, password);
                session.setAttribute("loginMember",member);
                result.put("code","0");
                result.put("msg","success");

            }catch (BussinessException ex){
                ex.printStackTrace();
                result.put("code",ex.getCode());
                result.put("msg",ex.getMsg());
            }

        }
        return  result;
    }

    @PostMapping("/update_read_state")
    @ResponseBody
    public Map updateReadState(Long memberId, Long bookId, Integer readState){
        Map result = new HashMap();
        try{
            memberService.updateMemberReadState(memberId,bookId,readState);
            result.put("code",0);
            result.put("msg","success");
        }catch (BussinessException ex){
            ex.printStackTrace();
            result.put("code",ex.getCode());
            result.put("msg",ex.getMsg());
        }
        return result;

    }

    @PostMapping("/evaluate")
    @ResponseBody
    public Map evaluate(Long memberId, Long bookId, Integer score, String content){
        Map result = new HashMap();
        try{
            //返回了evaluate对象，没有进行调用处理，这样是方便扩展处理
            //后续需要用到这个数据就接收，否则就不用接收
            memberService.evaluate(memberId,bookId,score,content);
            result.put("code",0);
            result.put("msg","success");
        }catch (BussinessException ex){
            ex.printStackTrace();
            result.put("code",ex.getCode());
            result.put("msg",ex.getMsg());
        }
        return result;
    }
    @PostMapping("/enjoy")
    @ResponseBody
    public Map enjoy(Long evaluationId){
        Map result = new HashMap();
        try{
            //返回了evaluate对象，没有进行调用处理，这样是方便扩展处理
            //后续需要用到这个数据就接收，否则就不用接收
            Evaluation enjoy = memberService.enjoy(evaluationId);
            result.put("code",0);
            result.put("msg","success");
            result.put("evaluation",enjoy);
        }catch (BussinessException ex){
            ex.printStackTrace();
            result.put("code",ex.getCode());
            result.put("msg",ex.getMsg());
        }
        return result;
    }


}
